package com.cecsmsserve.service.impl;

import com.cecsmsserve.entity.Activity;
import com.cecsmsserve.entity.User;
import com.cecsmsserve.entity.UserActivity;
import com.cecsmsserve.entity.vo.MyActivity;
import com.cecsmsserve.entity.vo.SignedUserList;
import com.cecsmsserve.mapper.ActivityMapper;
import com.cecsmsserve.mapper.UserActivityMapper;
import com.cecsmsserve.mapper.UserMapper;
import com.cecsmsserve.service.IUserActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王郝浠
 * @since 2024-04-22
 */
@Service
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements IUserActivityService {

    @Autowired
    UserActivityMapper mapper;

    @Autowired
    ActivityMapper activityMapper;
    
    @Autowired
    UserMapper userMapper;

    @Override
    public CommonResult selectAll() {
        List<UserActivity> list=mapper.selectAll();
        CommonResult result=new CommonResult(list);
        if (list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectAllByPage(int current, int size) {
        int start=(current-1)*size;
        List<UserActivity> list=mapper.selectAllByPage(start,size);
        List<UserActivity> getTotal=mapper.selectList(null);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectAllByuId(int uId) {
        List<MyActivity> list=mapper.selectAllByuId(uId);
        CommonResult result=new CommonResult(list);
        if (list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByUIdBymyState(int uId, String state) {
        List<MyActivity> list=mapper.selectByUIdBymyState(uId,state);
        CommonResult result=new CommonResult(list);
        if (list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByuIdByaId(int uId, int aId) {
        UserActivity userActivity=mapper.selectByuIdByaId(uId,aId);
        CommonResult result=new CommonResult(userActivity);
        if(userActivity==null){
            result.setNotFound();
        }
        return result;
    }

    public List selectUserList(int aId){
        List list=mapper.selectUserByaId(aId);
        List<SignedUserList> userList = new ArrayList<>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                int id= (int) list.get(i);
                SignedUserList user=userMapper.selectUserList(id);
                userList.add(user);
            }
        }
        return userList;
    }

    @Override
    public CommonResult selectUserByaId(int aId) {
        List<User> userList = selectUserList(aId);
        CommonResult result=new CommonResult();
        if(userList.size()>0){
            result.setOK(userList);
        }else{
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByUIdByState(int uId, String state) {
        List<MyActivity> list=mapper.selectByUIdByState(uId,state);
        CommonResult result=new CommonResult(list);
        if (list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult insert(UserActivity userActivity) {
        userActivity.setId(null);
        int aId=userActivity.getaId();
        Activity activity=activityMapper.selectById(aId);
        System.out.println(activity);
        int limitNum=activity.getLimitNum();
        int signNum=activity.getSignNum();
        CommonResult result=new CommonResult();
        if(limitNum==0||signNum<limitNum){//已报名人数<限制人数
            userActivity.setState("报名成功");
            int i=mapper.insert(userActivity);
            if(i<0){
                result.setNotInserted();
            }else{
                result.setOK(userActivity);
                activity.setSignNum(signNum+1);
                System.out.println(activity.getSignNum());
                activityMapper.updateById(activity);
            }
        }else {//报名失败
            result.setNotInserted();
        }

        return result;
    }

    @Override
    public CommonResult update(UserActivity userActivity) {
        int i=mapper.updateById(userActivity);
        int aId=userActivity.getaId();
        List list=mapper.selectUserByaId(aId);
        Activity activity=activityMapper.selectById(aId);
        activity.setSignNum(list.size());
        int j=activityMapper.updateById(activity);
        CommonResult result=new CommonResult(i);
        if(i<=0||j<=0){
            result.setNotUpdate();
        }
        return result;
    }
}
